using System.IO;
using System.Text;
using UnityEditor;
using UnityEditor.ProjectWindowCallback;
using System;
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;

public class ControllerCreateScript : EndNameEditAction
{
    public static string objName { set; get; }

    public override void Action(int instanceId, string pathName, string resourceFile)
    {
        string text = File.ReadAllText(resourceFile);
        string className = Path.GetFileNameWithoutExtension(pathName);
        string uiName = className.Replace("Ctrl", string.Empty);
        string modelName = objName.Substring(0, objName.Length - 2) + "Model";
        //string viewName = objName.Substring(0, 1).ToLower() + objName.Substring(1, objName.Length - 1);
        //string modelVarName = modelName.Substring(0, 1).ToLower() + modelName.Substring(1, modelName.Length - 1);
        string tab = "   ";
        className = className.Replace(" ", "");

        string OpenMsg = "(UICtrlMsg."+ uiName + "_Open,OpenUI);";
        string CloseMsg = "(UICtrlMsg." + uiName + "_Close,CloseUI);";
        string Add = tab+tab + "uiCtrlDispatcher.AddListener";
        string Remove= tab+tab + "uiCtrlDispatcher.RemoveListener";


        string AddListenStr = Add+ OpenMsg + "\n" +"//" + Add+ CloseMsg + "\n";
        string RemoveListenStr= Remove + OpenMsg + "\n" + "//" + Remove + CloseMsg + "\n";

        text = "using FutureCore;\n\n";
        text += "public class " + className + " : BaseUICtrl\n{\n" + tab + "";
        text += "#region BaseUICtrl\n" + tab + "";
        text +="//"+ modelName + " model;\n" + tab + "";
        text += objName + " ui;\n\n" + tab + "";
        text += "protected override void OnInit()\n" + tab + "{\n" + tab + "" + tab + "//model = ModuleMgr.Instance.GetModel(ModelConst." + modelName + ") as "+modelName + ";\n"+tab +"}\n\n" + tab + "";
        text += "protected override void OnDispose()\n" + tab + "{\n" + tab + "}\n\n" + tab + "";
        text += "protected override void OpenUI(object args = null)\n" + tab + "{\n" + tab + "" + tab + " ui = new " + objName + "(this);\n" + tab + "" + tab + " ui.Open(args);\n" + tab + "}\n\n" + tab + "";
        text += "protected override void AddListener()\n" + tab + "{\n" + AddListenStr + tab + "}\n\n" + tab + "";
        text += "protected override void RemoveListener()\n" + tab + "{\n" + RemoveListenStr + tab +  "}\n" + tab + "";
        text += "protected override void AddServerListener()\n" + tab + "{\n" + tab + "}\n\n" + tab + "";
        text += "protected override void RemoveServerListener()\n" + tab + "{\n" + tab + "}\n\n" + tab + "";
        text += @" private void CloseUI(object param)
        {
            if (ui != null && ui.gameObject != null)
            {
                ui.Close();
                ui = null;
            }
        }
        ";
        text += "#endregion\n}";

        UTF8Encoding encoding = new UTF8Encoding(true, false);

        File.WriteAllText(pathName, text, encoding);

        AssetDatabase.ImportAsset(pathName);
        MonoScript asset = AssetDatabase.LoadAssetAtPath<MonoScript>(pathName);
        ProjectWindowUtil.ShowCreatedAsset(asset);
    }
}